package com.zz.bsea.config;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Value("${imagesPath}")
    private String mImagesPath;
	private Logger log=Logger.getLogger(getClass());
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if(mImagesPath.equals("") || mImagesPath.equals("${imagesPath}")){
               String imagesPath = WebAppConfig.class.getClassLoader().getResource("").getPath();
               log.info("WebAppConfig--imagesPath--"+imagesPath);
               if(imagesPath.indexOf(".jar")>0){
                imagesPath = imagesPath.substring(0, imagesPath.indexOf(".jar"));
               }else if(imagesPath.indexOf("classes")>0){
                imagesPath = "file:"+imagesPath.substring(0, imagesPath.indexOf("classes"));
               }
               log.info("WebAppConfig--imagesPath2--"+imagesPath);
               imagesPath = imagesPath.substring(0, imagesPath.lastIndexOf("/"))+"/images/";
               mImagesPath = imagesPath;
              }
        log.info("WebAppConfig--mImagesPath3--"+mImagesPath);
              //LoggerFactory.getLogger(WebAppConfig.class).info("imagesPath="+mImagesPath);
              registry.addResourceHandler("/images/**").addResourceLocations(mImagesPath);
        // TODO Auto-generated method stub
        super.addResourceHandlers(registry);
    }
}